package day0805;

import java.util.Scanner;

/**
 * 华为8.4机试  方法2： 输入方法不同
 *
 * 输入：列 行
 * 5 5
 * 0 1 1 0 0
 * 0 1 1 0 0
 * 0 0 0 0 0
 * 0 0 1 2 3
 * 0 0 1 3 9
 * 输出： 19
 * */

//参考：求岛屿的最大面积  基本相同
//https://leetcode-cn.com/problems/max-area-of-island/
//输入：
//[[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]]
//输出：6
public class DFS2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int col = sc.nextInt();
        int row = sc.nextInt();
        int n = col*row;
        int[][] grid = new int[row][col];
        while(n>0){
            for (int i = 0; i <row ; i++) {
                for (int j = 0; j <col ; j++) {
                    grid[i][j] = sc.nextInt();
                    n--;
                }
            }
        }
        System.out.println(new DFS2().maxAreaOfIsland(grid));

//        for (int a = 0; a <row ; a++) {
//            for (int j = 0; j <col ; j++) {
//                System.out.print(grid[a][j]+" ");
//            }
//            System.out.println();
//        }



    }
    public int maxAreaOfIsland(int[][] grid) {
        int ans =0;
        for(int i=0;i<grid.length;i++){
            for(int j=0;j<grid[0].length;j++){
                if(grid[i][j] != 0){
                    ans = Math.max(ans,dfs(i,j,grid));
                }
            }
        }
        return ans;
    }
    public int dfs(int i,int j,int[][]grid){
        if(i<0||j<0||i>=grid.length||j>=grid[0].length||grid[i][j]==0){
            return 0;
        }
        int num =grid[i][j];
        grid[i][j]=0;//每次找到岛屿，则直接把找到的岛屿改成0，这是传说中的沉岛思想。

        num += dfs(i+1,j,grid);//右
        num += dfs(i-1,j,grid);//左
        num += dfs(i,j-1,grid);//上
        num += dfs(i,j+1,grid);//下
        return num;
    }
}
